useViewerContext
The useViewerContext hook provides access to the current state of the 3D scene in the primary view of the Viewer (commonly the 3D view). This hook is particularly useful for interacting with the scene graph, such as retrieving mesh data for specific objects.
Package
@promaton/scan-viewer
Interface: ViewerContext
The useViewerContext hook exposes the following properties and methods:
Properties
getMeshByObjectId
getMeshByObjectId: (objectId: string, scene?: Scene) =>
null |
Mesh<
BufferGeometry<NormalBufferAttributes>,
Material | Material[],
Object3DEventMap
>;
Retrieves a Mesh by its object ID from the specified scene. This function is effective only when the mesh is present in the scene graph. If the geometry of the mesh is required, it is recommended to use ViewerObjectUtils.getGeometryForViewerObject instead.
- Parameters:
objectId(string): The ID of the object whose mesh is to be retrieved.scene?(Scene): The scene to search in. If not provided, the primary scene is used.
- Returns:
nullif the mesh is not found.- A
Meshobject if the mesh is found.
scene
scene: null | Scene;
Represents the scene of the primary viewport in the Viewer. Each view in the Viewer has its own Scene.
Methods
setScene
setScene: (scene: null | Scene) => void
Updates the scene for the primary viewport.
- Parameters:
scene(null | Scene): The new scene to set.
- Returns:
void
Example Usage
Here’s an example of how to use useViewerContext:
import { useViewerContext } from "@promaton/scan-viewer";
const MyComponent = () => {
const { getMeshByObjectId, scene, setScene } = useViewerContext();
const handleRetrieveMesh = (objectId) => {
const mesh = getMeshByObjectId(objectId, scene);
if (mesh) {
console.log("Mesh found:", mesh);
} else {
console.log("Mesh not found.");
}
};
const handleUpdateScene = (newScene) => {
setScene(newScene);
console.log("Scene updated.");
};
return (
<div>
<button onClick={() => handleRetrieveMesh("object-id-123")}>
Retrieve Mesh
</button>
<button onClick={() => handleUpdateScene(null)}>Reset Scene</button>
</div>
);
};
This example demonstrates how to retrieve a mesh by its object ID and how to update the scene using the useViewerContext hook.